﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Pageparkproject.Models;

namespace Pageparkproject.Models
{
    public class Stories
    {
        PageParkDBEntities Db = new PageParkDBEntities();
        #region [Project]
        public List<tblProject> projectList()
        {
            return Db.tblProjects.Where(m => m.bStatus == true).ToList();
        }
        public List<tblProject> projectList(int id)
        {
            if (id != 0)
            {
                return Db.tblProjects.Where(m => m.bStatus == true).Where(m => m.FK_iCategoryID == id).ToList();
            }
            return projectList();
        }
        public tblProject projectGetById(int id)
        {
            return projectList().First(m => m.PK_iProjectID == id);
        }
        public List<tblImageProject> imageProjectList(int projectID)
        {
            return Db.tblImageProjects.Where(m => m.FK_iProjectID == projectID).ToList();
        }
        public List<tblProject> projectListFilter(int? categoryID, int? yearStart, int? yearEnd)
        {
            var listItem = projectList();
            if (categoryID != null) listItem = listItem.Where(m => m.FK_iCategoryID == categoryID).ToList();
            return listItem;
        }
        public tblProject getNextProject(tblProject currProject)
        {
            var listProject = projectList();
            for (int i = 0; i < listProject.Count; i++)
            {
                if (listProject[i].Equals(currProject))
                {
                    try
                    {
                        return listProject[i + 1];
                    }
                    catch
                    {
                        return listProject[0];
                    }
                }
            }
            return listProject[0];
        }
        public List<tblCategory> getListCategoryProject()
        {
            return Db.tblCategories.Where(m => m.bStatus == true).ToList();
        }
        #endregion

        #region [News]
        public List<tblCategoryNew> getListCategoryNews()
        {
            return Db.tblCategoryNews.Where(m => m.bStatus == true).ToList();
        }

        public List<tblNew> newsList(int id)
        {
            if (id != 0)
            {
                return Db.tblNews.Where(m => m.FK_iCategoryNewsID == id).Where(m => m.bStatus == true).ToList();
            }
            return Db.tblNews.Where(m => m.bStatus == true).ToList();
        }
        public List<tblNew> newsListFilter(int? year)
        {
            var newsList = this.newsList(0);
            if (year != null) newsList = newsList.Where(m => m.dDatepost.Year == year).ToList();
            return newsList;
        }
        public tblNew newsGetByID(int id)
        {
            return (Db.tblNews.First(m => m.PK_iNewsID == id));
        }
        public tblNew getLatestNews()
        {
            return Db.tblNews.OrderByDescending(m => m.dDatepost).First();
        }
        #endregion

        #region [Architecture]
        public tblCategory architectureGetByID(int id)
        {
            return Db.tblCategories.FirstOrDefault(m => m.PK_iCategoryID == id);
        }
        #endregion
        #region [Setting]
        public static tblSetting GetPageSetting()
        {
            var db = new PageParkDBEntities();
            return db.tblSettings.First();
        }
        #endregion
    }
}